import json, codecs, time, os
from pathlib import Path
from dotenv import load_dotenv

load_dotenv()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-%0!utdel#n1(go$pg7g8l)ao7(fn07usuyn$h*9nk#5vga$%rx'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_crontab',
    'corsheaders',
    'rest_framework',
    'complaint',
    'idmi_cuzu',
    'maixun_cuzu',
    'maixun_fetch',
]

REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True

ROOT_URLCONF = 'idmi_backend.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'idmi_backend.wsgi.application'


# Database PG-SQL
DATABASES = {
    'default': {
        # 生产时候使用的配置
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'HOST': os.getenv('DB_HOST'),
        'PORT': os.getenv('DB_PORT'),

        # 开发时候的配置
        # 'ENGINE': 'django.db.backends.postgresql',
        # 'NAME': 'dev',
        # 'USER': 'postgres',
        # 'PASSWORD': 'liuyang177',
        # 'HOST': '0.0.0.0',
        # 'PORT': 5433,
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_TZ = True

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# 定时任务
CRONJOBS = (
    ('10 4 * * *', 'tickets.TranslateView.translate_cuzu'),
    ('50 4 * * *', 'tickets.TranslateView.translate_complaints'),

    # 每 天 晚上执行一次翻译，翻译过去一天未能正确翻译的数据
)

# 脉讯对应的接口密钥
HUAWEI_OBS_AK = os.getenv('HUAWEI_OBS_AK')
HUAWEI_OBS_SK = os.getenv('HUAWEI_OBS_SK')
HUAWEI_OBS_ENDPOINT = os.getenv('HUAWEI_OBS_ENDPOINT')

# 数据翻译密钥和地址
TRANSLATE_URL = os.getenv('TRANSLATE_URL')
TRANSLATE_SK = os.getenv('TRANSLATE_SK')

# IDMI 后端 DJANGO 配置类型文件的读取
with codecs.open(
        filename='idmi_backend/data_configs/sourceId2websites.json',
        mode='r', encoding='utf-8'
) as fr:
    '''
    ID.MI 爬取的质量投诉数据和 websites 之间的对应关系 
    '''
    IDMI_SOURCEID2WEBSITES = json.load(fp=fr)
with codecs.open(
        filename='idmi_backend/data_configs/maixun_fid_20250717.json',
        mode='r', encoding='utf-8'
) as fr:
    '''
    脉讯数据源的 fid
    '''
    FID2INFO = {}
    _ = json.load(fp=fr)
    for key, value in _.items():
        FID2INFO[int(key)] = value

with codecs.open(
        filename='idmi_backend/data_configs/quality_labels_20250627.json',
        mode='r', encoding='utf-8'
) as fr:
    '''
    质量投诉的相关标签体系
    '''
    QUALITY_LABELS = json.load(fp=fr)
with codecs.open(
        filename='idmi_backend/data_configs/cuzu_labels_20250627.json',
        mode='r', encoding='utf-8'
) as fr:
    '''
    Customer Satisfaction 相关的标签体系
    '''
    CUZU_LABELS = json.load(fp=fr)

with codecs.open(
        filename='idmi_backend/data_configs/model_config_DICT.json',
        mode='r', encoding='utf-8'
) as fr:
    '''
    model_config_DICT
    '''
    MODEL_CONFIG = json.load(fp=fr)
with codecs.open(
        filename='idmi_backend/data_configs/maixun_domain.json',
        mode='r', encoding='utf-8'
) as fr:
    '''
    model_config_DICT
    '''
    MAIXUN_DOMAIN_CONFIG = json.load(fp=fr)

